package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQuery;
import com.atguigu.eduservice.service.EduTeacherService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author WFH
 * @since 2020-10-05
 */
@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public R findAll() {
        //因为mybatis-plus的service实现类自动帮我们注入了mapper所以不需要自己再注入mapper
        //调用service的方法实现查询所有的操作
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("items", list);
    }

    /**
     * 删除讲师
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    //@ApiParam给参数id一个友好的提示(swagger)
    public R removeEduTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean flag = eduTeacherService.removeById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }
    /**
     * 分页查询讲师的方法
     * current当前页数
     * limit每页显示记录数
     */
    @ApiOperation(value = "分页查询讲师的方法")
    @GetMapping("/pageTeancher/{current}/{limit}")
    public R pageTeancher(@PathVariable long current,@PathVariable long limit) {
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        //其实返回值对象的page里面也有分页所有数据，个人比较喜欢用page
        IPage<EduTeacher> page = eduTeacherService.page(pageTeacher, null);

        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords();//数据list集合
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    /**
     * 条件查询带分页方法
     * @RequestBody 使用json数据格式进行传递，为了与前端更好的交流
     * required = false 因为条件查询，条件不是必须的
     * @PostMapping 如果使用了@requestBody 那么必须使用post方式查询，不然查不到数据
     * 条件查询:1.教师名称,模糊查询
     *         2.头衔 1高级讲师 2首席讲师
     *         3.查询开始时间
     *         4.查询结束时间
     */
    @ApiOperation(value = "条件查询带分页方法")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery) {

        //调用方法实现分页
        Page<EduTeacher> pageTeacher = new Page<>(current,limit);

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();

        //对前端传来的条件进行判断,column是数据库字段名，而不是实体属性名
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //条件进行判断，如果符合则拼接
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level) ) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

        //根据时间降序排序
        wrapper.orderByDesc("gmt_create");


        //分页组合条件查询
        IPage<EduTeacher> page = eduTeacherService.page(pageTeacher, wrapper);
        long total = page.getTotal();//总记录数
        List<EduTeacher> records = page.getRecords();//数据list集合
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    /**
     * 添加讲师的接口方法
     */
    @ApiOperation(value = "添加讲师的接口方法")
    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = eduTeacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    /**
     * 根据讲师id进行查询
     */
    @ApiOperation(value = "根据讲师id进行查询")
    @GetMapping("/getTeacherById/{id}")
    public R getTeacherById(@PathVariable String id){
        //故意制造异常，测试异常处理
        /*try {
            int i = 10/0;
        }catch (Exception e){
            throw new GuliException(20001,"执行了自定义异常");
        }*/

        EduTeacher byId = eduTeacherService.getById(id);
        return R.ok().data("teacher", byId);
    }

    /**
     * 根据id修改讲师
     */
    @ApiOperation(value = "根据id修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }
    }





}

